跳到主要内容

字体(Font)

用于整形并绘制文本的字体。字体可来自文件资源(context:font(name))、视图模型的 Property<Font>,或通过 Font.decode 从字节解码。度量针对 1 point 字体,向上为负,因此需乘以字号,并预期 ascent 小于零。

type Label = {
text: Text,
}

function init(self: Label, context: Context): boolean
local font = context:font('Inter')
if font then
self.text:append('Hello', { font = font, size = 24, paint = Paint.new() })
end
return true
end

字段(Fields)​

ascent​

1 point 字体从基线到 em 框顶部的距离,为负。

descent​

1 point 字体从基线到 em 框底部的距离。

capHeight​

从基线到大写字母顶部的距离,为负。

xHeight​

从基线到小写字母顶部的距离,为负。

weight​

字重,范围 1 到 1000,400 为常规。

isItalic​

是否为斜体。

axes​

字体提供的可变轴。

features​

字体提供的四字母 OpenType 特性标签,例如 'liga'。

静态函数(Static Functions)​

decode​

decode(bytes: buffer) -> Font?

从脚本自行获得的字节(例如下载的文件)解码 TrueType 或 OpenType 字体。项目中的字体文件是字体资源,应通过 context:font 获取,而不是 Blob。当字节不是字体时返回 nil。

local font = Font.decode(downloadedBytes)

方法(Methods)​

lineHeight​

lineHeight(size: number) -> number

给定字号下一行文本的高度。

hasGlyph​

hasGlyph(codepoint: number) -> boolean

字体是否包含该 Unicode 码点对应的字形。

axisValue​

axisValue(tag: string) -> number

按标签返回可变轴的当前值。

withOptions​

withOptions(axes: {[string]: number}?, features: {[string]: number}?) -> Font

返回使用给定轴值与特性设置的新字体。

local bold = font:withOptions({ wght = 700 }, { liga = 0 })

glyphPath​

glyphPath(glyphId: number) -> Path

单个字形的轮廓,单位为 1 point,基线在 y 0,绕向适合顺时针填充。绘制时按字号缩放。

看完还有疑问?进群交流下!
与众多 Rive 创作者、开发者一起交流探讨与答疑解惑。
加入交流群